$(function () {

    //渲染数据
    initArtCateList()

    function initArtCateList() {
        axios({
            url: '/my/article/cates',
        }).then(res => {
            //成功回调

            let dataArr = res.data.data
            if (res.data.status != 0) return layer.msg(res.data.message)
            //渲染
            let htmlArr = []
            dataArr.forEach(value => {
                htmlArr.push(`
                    <tr>
                        <td>${value.Id}</td>
                        <td>${value.name}</td>
                        <td>${value.alias}</td>
                        <td>
                            <button type="button" data-id="${value.Id}" id="amend" class="layui-btn layui-btn-xs">修改</button>
                            <button type="button" data-id="${value.Id}" class="layui-btn layui-btn-xs layui-btn-danger del-edit">删除</button>
                        </td>
                    </tr>
                `)
            })
            $('tbody').empty().html(htmlArr)

        })
    }
    //新增弹框
    let indexAdd = 0
    const layer = layui.layer
    $('#addBtn').on('click', function () {
        //弹出框
        indexAdd = layer.open({
            title: '在线调试',
            type: 1,
            area: ['500px', '300px'],
            content: `
                    <form id="addForm" class="layui-form" action="">
                            <!-- 1.分类 -->
                            <div class="layui-form-item">
                                <label class="layui-form-label">分类名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                                        class="layui-input">
                                </div>
                            </div>
                            <!-- 2.别名 -->
                            <div class="layui-form-item">
                                <label class="layui-form-label">分类别名</label>
                                <div class="layui-input-block">
                                    <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                                        class="layui-input">
                                </div>
                            </div>
                            <!-- 3.按钮 -->
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn" lay-submit lay-filter="formDemo">确认添加</button>
                                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                </div>
                            </div>
                </form>`
        })
        //给body添加注册事件委托给动态新增的form表单
        $('body').on('submit', '#addForm', function (e) {
            e.preventDefault()
            axios({
                url: '/my/article/addcates',
                method: 'post',
                data: $(this).serialize()

            }).then(res => {
                if (res.data.status != 0) return layer.msg(res.data.message)
                //成功返回
                layer.msg(res.data.message)
                // 渲染页面
                initArtCateList()
                //关闭新增页面
                layer.close(indexAdd)
            })
        })
    })
    //修改分类弹出层
    let indexAmend = 0
    $('body').on('click','#amend',function(){
         //弹出框
         indexAmend = layer.open({
            title: '在线调试',
            type: 1,
            area: ['500px', '300px'],
            content: `
                    <form id="amendForm" class="layui-form" action="" lay-filter="editForm">
                    <input type="hidden" name="Id">
                            <!-- 1.分类 -->
                            <div class="layui-form-item">
                                <label class="layui-form-label">分类名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                                        class="layui-input">
                                </div>
                            </div>
                            <!-- 2.别名 -->
                            <div class="layui-form-item">
                                <label class="layui-form-label">分类别名</label>
                                <div class="layui-input-block">
                                    <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                                        class="layui-input">
                                </div>
                            </div>
                            <!-- 3.按钮 -->
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn" lay-submit lay-filter="formDemo">确认修改</button>
                                </div>
                            </div>
                </form>`
        })
        //给body添加注册事件委托给动态新增的form表单
        let id = $(this).attr('data-id')
        axios({
            url:'/my/article/cates/' + id,
            method:'get',
            
        }).then(res=>{
            //成功回调
            layui.form.val('editForm', res.data.data)
        })

    })

    $('body').on('submit','#amendForm',function(e){
        e.preventDefault()

        axios({
            url:'/my/article/updatecate',
            method:'post',
            data: $(this).serialize()
        }).then(res=>{
            //成功回调
             layer.msg(res.data.message)
             //渲染数据
             initArtCateList()
             //关闭弹窗
             layer.close(indexAmend)
        })
    })


    $('body').on('click','.del-edit',function(e){
        e.preventDefault()
        let id = $(this).attr('data-id')
        axios({
            url:'/my/article/deletecate/' + id,
            method:'get',
        }).then(res=>{
            //成功回调
            console.log(res)
            layer.msg(res.data.message)
            //渲染数据
            initArtCateList()
            
            
        })
    })





})